package com.lzj.weather01;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class Weather01Reducer extends Reducer<Text, IntWritable, Text, Text> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //声明变量分别存放最低温度和最高温度
        int high = Integer.MAX_VALUE;
        int low = Integer.MAX_VALUE;
        //获取迭代器
        Iterator<IntWritable> iterator = values.iterator();
        //开始遍历
        for (IntWritable value : values) {
            //获取温度
            int temperature = value.get();
            //判断并比较
            if (high == Integer.MAX_VALUE) {
                high = temperature;
            } else if (high < temperature) {
                high = temperature;
            }
            if (low == Integer.MAX_VALUE) {
                low = temperature;
            } else if (low > temperature) {
                low = temperature;
            }
        }
        //最终将数据写出
        context.write(key, new Text("最高温度【" + high + "】最低温度【" + low + "】"));
    }
}
